﻿using _IMES.DBUtils;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using houseAgency.Models;
using houseAgency.View.UserInfor;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace houseAgency.ViewModel.UserInfor
{
    public class PeopleListViewModel : ObservableObject
    {

        public PeopleListViewModel()
        {
            PeopleList = GetUserByType("lend");
        }

        #region 属性
        private string? id;

        public string? ID
        {
            get { return id; }
            set
            {
                id = value;
                OnPropertyChanged();
            }
        }
        private string? name;

        public string? Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged();
            }
        }
        private string? homePhone;

        public string? HomePhone
        {
            get { return homePhone; }
            set
            {
                homePhone = value;
                OnPropertyChanged();
            }
        }
        private string? cardID;

        public string? CardID
        {
            get { return cardID; }
            set
            {
                cardID = value;
                OnPropertyChanged();
            }
        }
        private string? phone;

        public string? Phone
        {
            get { return phone; }
            set
            {
                phone = value;
                OnPropertyChanged();
            }
        }

        private List<User>? peopleList = new();

        public List<User>? PeopleList
        {
            get { return peopleList; }
            set
            {
                peopleList = value;
                OnPropertyChanged();
            }
        }

        private User? selectedUser;

        public User? SelectedUser
        {
            get { return selectedUser; }
            set
            {
                selectedUser = value;
                if (value != null)
                {
                    ID = value.ID;
                    Name = value.Name;
                    HomePhone = value.HomePhone;
                    CardID = value.CardID;
                    Phone = value.Phone;
                }
                OnPropertyChanged();
            }
        }

        private int currentTabIndex;

        public int CurrentTabIndex
        {
            get { return currentTabIndex; }
            set
            {
                currentTabIndex = value;
                OnPropertyChanged();
            }
        }


        #endregion

        #region 命令 Command
        public ICommand FindCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    User user = new() { ID = ID ?? "", Name = Name ?? "", HomePhone = HomePhone ?? "", CardID = CardID ?? "", Phone = Phone ?? "" };
                    if (CurrentTabIndex == 0)
                    {
                        PeopleList = FindUser(user, "lend");
                    }
                    else if (CurrentTabIndex == 1)
                    {
                        PeopleList = FindUser(user, "want");
                    }
                });
            }
        }
        public ICommand ChangeCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (ID != null && ID != "") 
                    {
                        if (MessageBox.Show(String.Format("您确定要修改编号为：{0}的用户信息吗？", ID), "修改操作", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
                        {
                            User user = new() { ID = ID ?? "", Name = Name ?? "", HomePhone = HomePhone ?? "", CardID = CardID ?? "", Phone = Phone ?? "" };
                            bool result = UpdateUser(user);
                            if (result)
                            {
                                MessageBox.Show("修改成功");
                            }
                            else
                            {
                                MessageBox.Show("修改失败");
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("请选择要修改的用户信息！！");
                    }
                    
                });
            }
        }
        public ICommand DelCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (MessageBox.Show(String.Format("您确定要删除编号为：{0}的用户吗？", ID), "删除操作", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
                    {
                        bool result = DeleteUser(ID ?? "");
                        if (result)
                        {
                            MessageBox.Show("删除成功");
                        }
                        else
                        {
                            MessageBox.Show("删除失败");
                        }
                    }
                });
            }
        }
        public ICommand RefreshCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (CurrentTabIndex == 0)
                    {
                        PeopleList = FindUserByType("lend");
                    }
                    else if (CurrentTabIndex == 1)
                    {
                        PeopleList = FindUserByType("want");
                    }
                });
            }
        }

        public ICommand SelectedItemChangedCommand
        {
            get
            {
                return new RelayCommand<object>(o =>
                { 
                    if (o is int num)
                    {
                        if (num == 0)
                        {
                            PeopleList = GetUserByType("lend");
                        }
                        else if (num == 1)
                        {
                            PeopleList = GetUserByType("want");
                        }
                        CurrentTabIndex = num;
                        ID = "";
                        Name = "";
                        HomePhone = "";
                        CardID = "";
                        Phone = "";
                    }
                });
            }
        }
        #endregion

        #region 方法
        private List<User> GetUserByType(string v)
        {
            string sql = "select id,name,homePhone,cardID,phone from user where type = @type order by id";
            DataTable gpParaTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql, new MySqlParameter("type",v));
            return DBConvert.DataTableToList<User>(gpParaTable);
        }
        private List<User> FindUser(User user, string type)
        {
            string sql = "select id,name,homePhone,cardID,phone from user where type = @type and id like @id and name like @name and homePhone like @homePhone and cardID like @cardID and phone like @phone order by id";
            DataTable gpParaTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql, new MySqlParameter[] { new MySqlParameter("type", type), new MySqlParameter("id", "%"+ user.ID+"%") , new MySqlParameter("name", "%" + user.Name + "%") , new MySqlParameter("homePhone", "%" + user.HomePhone + "%") , new MySqlParameter("cardID", "%" + user.CardID + "%") , new MySqlParameter("phone", "%" + user.Phone + "%") });
            return DBConvert.DataTableToList<User>(gpParaTable);
        }
        private bool UpdateUser(User user)
        {
            string sql = "update user set name = @name, homePhone = @homePhone, cardID = @cardID, phone = @phone where id = @id";
            int row = MySQLHelper.ExecuteNonQuery(MySQLHelper.DBConnectionString, sql, new MySqlParameter[] { new MySqlParameter("id", user.ID), new MySqlParameter("name", user.Name), new MySqlParameter("homePhone", user.HomePhone), new MySqlParameter("cardID", user.CardID), new MySqlParameter("phone", user.Phone)});
            if (row > 0)
                return true;
            return false;
        }
        private bool DeleteUser(string iD)
        {
            string sql = "delete from user where id = @id";
            int row = MySQLHelper.ExecuteNonQuery(MySQLHelper.DBConnectionString, sql, new MySqlParameter("id", ID));
            if (row > 0)
                return true;
            return false;
        }
        private List<User> FindUserByType(string type)
        {
            string sql = "select id,name,homePhone,cardID,phone from user where type = @type order by id";
            DataTable gpParaTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql, new MySqlParameter("type", type));
            return DBConvert.DataTableToList<User>(gpParaTable);
        }
        #endregion
    }
}
